-- @owner: @zou_jialiang050
-- @date: 2022/02/24
-- @testpoint: 创建truncate触发器 each row after truncate on带有when条件，合理报错

--step1:建表;expect:成功
drop table if exists t_trigger_src_51 cascade;
drop table if exists t_trigger_des_51 cascade;
drop sequence if exists test_trigger_des_51_seq1 cascade;
create table t_trigger_src_51(id1 int, id2 int, id3 int) with (storage_type=ustore);
create table t_trigger_des_51(id1 int, id2 int, id3 int) with (storage_type=ustore);

--step2:创建truncate触发器 each row after truncate on带有when条件;expect:合理报错
create sequence test_trigger_des_51_seq1;
create or replace function tri_truncate_func() returns trigger as
$$
declare
begin
insert into t_trigger_des_51 values(nextval('test_trigger_des_51_seq1'),nextval('test_trigger_des_51_seq1'),nextval('test_trigger_des_51_seq1'));
return new;
end
$$ language plpgsql;
/
drop trigger if exists truncate_trigger on t_trigger_src_51;
create trigger truncate_trigger after truncate on t_trigger_src_51
for each row
when(old.id1<3)
execute procedure tri_truncate_func();
/

--step3:清理环境;expect:成功
drop trigger if exists truncate_trigger on t_trigger_src_51;
drop table if exists t_trigger_src_51 cascade;
drop table if exists t_trigger_des_51 cascade;
drop sequence if exists test_trigger_des_51_seq1 cascade;
drop function if exists tri_truncate_func;